package com.railway.field.controller;

import com.railway.field.entity.User;
import com.railway.field.page.JsonResult;
import com.railway.field.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Api(description = "用户接口")
@RestController
@RequestMapping("User")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "智慧工地大屏登录接口",notes = "传参:username(String,账户),password(String,密码)")
    @PostMapping("/BigLogin")
    public JsonResult SelectUser(@RequestBody Map map) throws ParseException {
        JsonResult jsonResult = new JsonResult();
        User user = userService.SelectUser(map);
        if (user != null) {
            if (user.getPassword().equals(map.get("password"))) {
                // 获取当前时间
                Date date = new Date();
                //如果想比较日期则写成"yyyy-MM-dd"就可以了
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                //将字符串形式的时间转化为Date类型的时间
                Date a = sdf.parse(user.getEnd_time());
                Date b = sdf.parse(sdf.format(date));
                if (a.getTime() - b.getTime() >= 0) {
                    jsonResult.setData(user);
                    jsonResult.setMessage("登录成功!");
                    jsonResult.setCode(200);
                    return jsonResult;
                } else {
                    jsonResult.setMessage("账号已过期!");
                    jsonResult.setCode(20004);
                    return jsonResult;

                }
            } else {
                jsonResult.setMessage("密码错误,登录失败!");
                jsonResult.setCode(20003);
                return jsonResult;
            }
        } else {
            jsonResult.setMessage("账号不存在!");
            jsonResult.setCode(20001);
            return jsonResult;
        }
    }
}
